package com.yupi.springbootinit.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.interfacesubmit.InterfaceSubmitAddRequest;
import com.yupi.springbootinit.model.dto.interfacesubmit.InterfaceSubmitQueryRequest;
import com.yupi.springbootinit.model.dto.interfacesubmit.InterfaceSubmitUpdateRequest;
import com.yupi.springbootinit.model.entity.InterfaceSubmit;
import com.yupi.springbootinit.model.enums.UserRoleEnum;
import com.yupi.springbootinit.model.vo.InterfaceSubmitVO;
import com.yupi.springbootinit.service.InterfaceSubmitService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 接口提审相关接口
 * @author <a href="https://gitee.com/ca_40/capi">Calcium</a>
 */
@RequestMapping("/submit")
@RestController
@RequiredArgsConstructor
public class InterfaceSubmitController {

    private final InterfaceSubmitService interfaceSubmitService;

    /**
     * 提审接口
     * @param addRequest 提交的接口信息
     * @return ok
     */
    @AuthCheck(mustRole = "provider")
    @PostMapping("/add")
    public BaseResponse<Boolean> addInterfaceSubmit(@RequestBody InterfaceSubmitAddRequest addRequest,HttpServletRequest request) {
        if (ObjUtil.isEmpty(addRequest) || ObjUtil.isEmpty(request)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceSubmitService.addInterfaceSubmit(addRequest,request);
    }

    /**
     * 删除提审请求
     * @param id 接口id
     * @return ok
     */
    @AuthCheck(mustRole = "provider")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteInterfaceSubmit(Long id) {
        if (ObjUtil.isEmpty(id)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceSubmitService.deleteInterfaceSubmit(id);
    }
    /**
     * 更新提审请求
     * @param updateRequest 更新请求
     * @return ok
     */
    @AuthCheck(mustRole = "provider")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateInterfaceSubmit(@RequestBody InterfaceSubmitUpdateRequest updateRequest) {
        if (ObjUtil.isEmpty(updateRequest)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceSubmitService.updateInterfaceSubmit(updateRequest);
    }

    /**
     * 管理员分页获取所有提审请求
     * @param queryRequest 查询请求
     * @return 提审请求
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/list")
    public BaseResponse<Page<InterfaceSubmitVO>> listInterfaceSubmit(@RequestBody InterfaceSubmitQueryRequest queryRequest) {
        if (ObjUtil.isEmpty(queryRequest)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceSubmitService.listInterfaceSubmit(queryRequest);
    }

    /**
     * 根据id获取提审请求详情
     * @return
     */
    @AuthCheck(mustRole = "provider")
    @GetMapping("/list/{id}")
    public BaseResponse<InterfaceSubmit> getInterfaceSubmitDetail(@PathVariable("id") Long id) {
        if (ObjUtil.isEmpty(id)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceSubmitService.getInterfaceSubmitDetail(id);
    }

    /**
     * 供应商查询自己的提审
     * @param queryRequest 查询请求
     * @return 提审请求
     */
    @AuthCheck(mustRole = "provider")
    @PostMapping("/list/VO")
    public BaseResponse<Page<InterfaceSubmitVO>> getInterfaceSubmitVO(@RequestBody InterfaceSubmitQueryRequest queryRequest, HttpServletRequest request) {
        if (ObjUtil.isEmpty(queryRequest) || ObjUtil.isEmpty(request)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return interfaceSubmitService.getInterfaceSubmitVO(queryRequest,request);
    }

}


